package priv.bruce.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import priv.bruce.entry.AdvertisingInfo;
import priv.bruce.util.HandlerSet;
import priv.bruce.util.JDBCUtil;

public class AdvertisingServise implements HandlerSet<AdvertisingInfo>{

	public int addAD(String adName , String adPath , double pay , String picture) throws Exception {
		String sql = "INSERT INTO t_advertising(ad_name , ad_path , ad_money , ad_picture)"
				+ " values(?,?,?,?)";
		return JDBCUtil.update(sql, adName , adPath , pay , picture);
	}
	
	public List<AdvertisingInfo> findADByName(String adName) throws Exception{
		String sql = "SELECT * FROM t_advertising WHERE ad_name LIKE CONCAT('%',?,'%')";
		List<AdvertisingInfo> advertisingInfos = JDBCUtil.query(sql, this, adName);
		return advertisingInfos;
	}
	
	public List<AdvertisingInfo> orderAD() throws Exception{
		String sql = "SELECT * FROM t_advertising ORDER BY ad_money DESC";
		List<AdvertisingInfo> advertisingInfos = JDBCUtil.query(sql, this);
		return advertisingInfos;
	}
	
	@Override
	public AdvertisingInfo handler(ResultSet rs) throws SQLException {
		AdvertisingInfo advertisingInfo = new AdvertisingInfo();
		advertisingInfo.setAdId(rs.getInt("ad_id"));
		advertisingInfo.setAdName(rs.getString("ad_name"));
		advertisingInfo.setAdPath(rs.getString("ad_path"));
		advertisingInfo.setAdMoney(rs.getDouble("ad_money"));
		advertisingInfo.setAdPicture(rs.getString("ad_picture"));
		return advertisingInfo;
	}
}
